/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Dialogs;

import GUI.Components.BButton;
import GUI.Components.CancelButton;
import General.CurrentData;
import MonkeyStuff.B3DApplication;
import other.Wizard;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.util.concurrent.Callable;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jdesktop.swingx.VerticalLayout;

/**
 *
 * @author David
 */
public class AssetBrowserDialog extends BasicDialog
{

    private JTabbedPane tabbedPane = new JTabbedPane();
    private ModelFileBrowser modelFileBrowser = new ModelFileBrowser();
    private TextureFileBrowser textureFileBrowser = new TextureFileBrowser();
    private AudioFileBrowser audioFileBrowser = new AudioFileBrowser();
    private OtherFileBrowser otherFileBrowser = new OtherFileBrowser();
    private JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    private BButton addButton = new BButton("Add...");
    private BButton deleteButton = new BButton("Delete");
    private BButton refreshButton = new BButton("Refresh");
    private JCheckBox showPreviewCheckBox = new JCheckBox("Show Texture Preview");
    private CancelButton closeButton;
    private JPanel previewPanel = new JPanel(new VerticalLayout());
    private JLabel previewLabel = new JLabel();
    private String insertTextureName;

    /**
     *
     */
    public AssetBrowserDialog()
    {
        addWindowListener(new WindowListener()
        {
            public void windowOpened(WindowEvent e)
            {
            }

            public void windowClosing(WindowEvent e)
            {
                CurrentData.getEditorWindow().getMainMenu().getViewMenu().getShowAssetBrowserItem().setSelected(false);
                CurrentData.getConfiguration().getSettings().put("assetbrowsershown", "false");
                CurrentData.getConfiguration().save();
            }

            public void windowClosed(WindowEvent e)
            {
            }

            public void windowIconified(WindowEvent e)
            {
            }

            public void windowDeiconified(WindowEvent e)
            {
            }

            public void windowActivated(WindowEvent e)
            {
            }

            public void windowDeactivated(WindowEvent e)
            {
            }
        });
        closeButton = new CancelButton(this, "Close");
        closeButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CurrentData.getConfiguration().getSettings().put("assetbrowsershown", "false");
                CurrentData.getEditorWindow().getMainMenu().getViewMenu().getShowAssetBrowserItem().setSelected(false);
                CurrentData.getConfiguration().save();
            }
        });
        showPreviewCheckBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                CurrentData.getConfiguration().getSettings().put("showtexturepreview", Boolean.toString(showPreviewCheckBox.isSelected()));
                CurrentData.getConfiguration().save();
                if (showPreviewCheckBox.isSelected())
                {
                    AssetBrowserDialog.this.add(previewPanel, BorderLayout.EAST);
                } else
                {
                    AssetBrowserDialog.this.remove(previewPanel);
                }
                repaint();
                pack();
            }
        });
        tabbedPane.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                if (tabbedPane.getSelectedIndex() == 1 && showPreviewCheckBox.isSelected())
                {
                    add(previewPanel, BorderLayout.EAST);
                } else
                {
                    remove(previewPanel);
                }
            }
        });
        refreshButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                modelFileBrowser.refresh();
                textureFileBrowser.refresh();
                audioFileBrowser.refresh();
                otherFileBrowser.refresh();
            }
        });
        previewLabel.setPreferredSize(new Dimension(300, 300));
        tabbedPane.setPreferredSize(new Dimension(300, 300));
        tabbedPane.addTab("Models", modelFileBrowser);
        tabbedPane.addTab("Textures", textureFileBrowser);
        tabbedPane.addTab("Audio Files", audioFileBrowser);
        tabbedPane.addTab("Others", otherFileBrowser);
        controlPanel.add(addButton);
        controlPanel.add(deleteButton);
        controlPanel.add(refreshButton);
        controlPanel.add(showPreviewCheckBox);
        controlPanel.add(closeButton);
        previewPanel.add(new JLabel("Preview:"));
        previewPanel.add(previewLabel);
        add(tabbedPane, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.SOUTH);
        if (CurrentData.getConfiguration().getSettings().get("showtexturepreview").equals("true"))
        {
            add(previewPanel, BorderLayout.EAST);
            showPreviewCheckBox.setSelected(true);
        }
        pack();
        setTitle("Assets");
        setAlwaysOnTop(true);
        setModal(false);
        setLocation(0, 200);
        setVisible(CurrentData.getConfiguration().getSettings().get("assetbrowsershown").equals("true"));
    }

    private class ModelFileBrowser extends JPanel
    {

        public ModelFileBrowser()
        {
            refresh();
        }

        private void refresh()
        {
            removeAll();
            File directory = CurrentData.getProject().getAssetsFolder();
            setLayout(new GridLayout((int) Math.sqrt(directory.listFiles().length), (int) Math.sqrt(directory.listFiles().length) + 1));
            for (File file : directory.listFiles())
            {
                if (file.getName().endsWith(".blend") || file.getName().endsWith(".obj") || file.getName().endsWith(".j3o"))
                {
                    add(new ModelButton(file));
                }
            }
            repaint();
            revalidate();
            validate();
        }

        private class ModelButton extends BButton
        {

            private File file;

            public ModelButton(File f)
            {
                file = f;
                setText(file.getName().substring(0, file.getName().lastIndexOf(".")));
                addMouseListener(new MouseListener()
                {
                    public void mouseClicked(MouseEvent e)
                    {
                    }

                    public void mousePressed(MouseEvent e)
                    {
                    }

                    public void mouseReleased(MouseEvent e)
                    {
                        CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                        {
                            public Void call() throws Exception
                            {
                                CurrentData.getEditorWindow().getB3DApplication().setInsertAssetName(file.getName());
                                CurrentData.getEditorWindow().getB3DApplication().setInteractionType(B3DApplication.InteractionType.InsertModel, file.getName());
                                return null;
                            }
                        });
                    }

                    public void mouseEntered(MouseEvent e)
                    {
                    }

                    public void mouseExited(MouseEvent e)
                    {
                    }
                });
            }

            public File getFile()
            {
                return file;
            }
        }
    }

    private class AudioFileBrowser extends JPanel
    {

        public AudioFileBrowser()
        {
            refresh();
        }

        private void refresh()
        {
            removeAll();
            repaint();
            revalidate();
            validate();
        }
    }

    private class TextureFileBrowser extends JPanel
    {

        public TextureFileBrowser()
        {
            refresh();
        }

        private void refresh()
        {
            removeAll();
            File directory = CurrentData.getProject().getAssetsFolder();
            setLayout(new GridLayout((int) Math.sqrt(directory.listFiles().length), (int) Math.sqrt(directory.listFiles().length) + 1));
            for (File file : directory.listFiles())
            {
                if (file.getName().endsWith(".png") || file.getName().endsWith(".jpg") || file.getName().endsWith(".gif") || file.getName().endsWith(".jpeg"))
                {
                    add(new TextureButton(file));
                }
            }
            repaint();
            revalidate();
            validate();
        }

        private class TextureButton extends BButton
        {

            private File file;

            public TextureButton(File f)
            {
                file = f;
                setText(file.getName());
                setIcon(Wizard.resizeImage(new ImageIcon(CurrentData.getProject().getAssetsFolder() + "//" + file.getName()).getImage(), 30, 30, false));
                setHorizontalTextPosition(BButton.CENTER);
                setVerticalTextPosition(BButton.BOTTOM);
                addMouseListener(new MouseListener()
                {
                    public void mouseClicked(MouseEvent e)
                    {
                    }

                    public void mousePressed(MouseEvent e)
                    {
                    }

                    public void mouseReleased(MouseEvent e)
                    {
                        CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                        {
                            public Void call() throws Exception
                            {
                                CurrentData.getEditorWindow().getB3DApplication().setInteractionType(B3DApplication.InteractionType.InsertTexture, file.getName());
                                return null;
                            }
                        });
                    }

                    public void mouseEntered(MouseEvent e)
                    {
                        if (showPreviewCheckBox.isSelected())
                        {
                            previewLabel.setIcon(Wizard.resizeImage(
                                    new ImageIcon(CurrentData.getProject().getAssetsFolder() + "//" + file.getName()).getImage(),
                                    previewLabel.getWidth(),
                                    previewLabel.getHeight(), false));
                        }
                    }

                    public void mouseExited(MouseEvent e)
                    {
                    }
                });
            }

            public File getFile()
            {
                return file;
            }
        }
    }

    private class OtherFileBrowser extends JPanel
    {

        public OtherFileBrowser()
        {
            refresh();
        }

        private void refresh()
        {
            removeAll();
            repaint();
            revalidate();
            validate();
        }
    }
}
